package com.jorado.etl.service;

import com.jorado.core.utility.IOUtils;
import com.jorado.core.utility.StringUtils;
import com.jorado.etl.model.EtlInput;
import com.jorado.event.EventClient;
import com.jorado.fasttext.FilterManager;
import com.jorado.lexicon.model.NatureUnit;
import com.jorado.lexicon.postag.PosTagger;
import com.jorado.lexicon.postag.impl.HanlpRecognizer;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * 职位语料提取服务
 * 转换，过滤，分词
 */
public class EtlService {

    static {
        PosTagger.getDefault().setRecognizer(new HanlpRecognizer());
    }

    private String outputFile;
    private FilterManager filterManager;

    public EtlService(String outputFile) {
        this.outputFile = outputFile;
        this.filterManager = new FilterManager();
    }

    public void save(HashSet<EtlInput> etlInputList) {

        String jobTitle;
        String jobType;
        String jobDescription;
        try {

            List<String> outputList = new ArrayList<>();

            for (EtlInput input : etlInputList) {

                StringBuilder sb = new StringBuilder();

                jobTitle = filter(input.getJobTitle());
                jobType = filter(input.getJobType());
                jobDescription = filter(input.getJobDescription());

                sb.append(jobTitle).append(" ");
                sb.append(jobType).append(" ");

                sb.append(segment(jobTitle)).append(" ");
                sb.append(segment(jobType)).append(" ");
                sb.append(segment(StringUtils.getChinese(jobDescription))).append(" ");
                String line = sb.toString();

                outputList.add(line);
            }
            IOUtils.appendLines(this.outputFile, outputList);
        } catch (Exception ex) {
            EventClient.getDefault().asyncSubmitException("Save etl output data error", ex);
        }
    }

    /**
     * 过滤
     *
     * @param input
     * @return
     */
    public String filter(String input) {
        return filterManager.filter(input);
    }

    /**
     * 分词
     *
     * @param input
     * @return
     */
    public String segment(String input) {
        List<NatureUnit> natureUnits = PosTagger.getDefault().recognition(input);
        StringBuilder sb = new StringBuilder();
        for (NatureUnit natureUnit : natureUnits) {
            String word = natureUnit.getWord();
            //剔除单个字
            if (word.length() > 1) {
                sb.append(natureUnit.getWord());
                sb.append(" ");
            }
        }
        return sb.toString();
    }
}
